﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MdcService.Configuration;
using MdcService.Machine;
using MdcService.Static;
using MdcService.Structure;
using MdcService.Structure.Enum;

namespace MdcService.Factory
{
	public static class MachineFactory
	{


		public static BaseMachine CreateInstance(string id, string name, string path, MachineType type, List<ValueItem> tags)
		{
			BaseMachine m;
			switch (type)
			{
				case MachineType.Testing:
					m = new TestingMachine
					{
						Id = id,
						Name = name,
						Path = path,
						Type = type,
					};
					tags.ForEach(i=>m.Values.Add(i.Path,i));
					return m;
				case MachineType.Siunmerik:
					m = new SinumerikMachine
					{
						Id = id,
						Name = name,
						Path = path,
						Type = type,
					};
					tags.ForEach(i => m.Values.Add(i.Path, i));
					return m;
				case MachineType.Moxa:
					m = new MoxaMachine
					{
						Id = id,
						Name = name,
						Path = path,
						Type = type,
					};
					tags.ForEach(i => m.Values.Add(i.Path, i));
					return m;
				default:
			throw new NotImplementedException($"Type of machine {type} not implemented");
		}
	}


	public static IEnumerable<BaseMachine> GetMachines(IEnumerable<MachineDefinition> machineDefinitions)
		{
			return machineDefinitions.Where(i => i.Enabled)
				.Select(i => CreateInstance(i.Id, i.Name, i.Path, i.Type, i.Tags.Select(j => new ValueItem
				{
					Path = j.Key,
					Description = j.Value,
				}).ToList()));
		}

		public static MachineDefinition CreateTestingMachineDefinition(string name = "",string path = "test@test")
		{
			var id = Utilities.NewId();
			return new MachineDefinition()
			{
				Id = id,
				Name = string.IsNullOrEmpty(name) ? id : name,
				Path = path,
				Enabled = true,
				Type = MachineType.Testing,
				Tags = Enumerable.Range(1, 8).ToDictionary(i => $"Var{i}", i => $@"第{i}个变量")
			};
		}

		public static MachineDefinition CreateMoxaMachineDefinition(string path, string name = "")
		{
			var id = Utilities.NewId();
			return new MachineDefinition()
			{
				Id = id,
				Name = string.IsNullOrEmpty(name) ? id : name,
				Path = path,
				Enabled = true,
				Type = MachineType.Moxa,
				Tags = "DI DIO AI".Split(' ')
					.Select(i => Enumerable.Range(1, 4).Select(j => $"{i}{j}"))
					.SelectMany(i => i).ToDictionary(i => i, i => $"变量{i}")
			};
		}

		public static MachineDefinition CreateSinumerikMachineDefinition(string path, string name = "")
		{
			var id = Utilities.NewId();
			return new MachineDefinition()
			{
				Id = id,
				Name = string.IsNullOrEmpty(name) ? id : name,
				Path = path,
				Enabled = true,
				Type = MachineType.Siunmerik,
				Tags = Resource1.SinumerikTagString.Split('\n')
					.Select(i =>
					{
						var a = i.Split('=');
						return new[] { a[0].Trim(), a[1].Trim() };
					})
					.ToLookup(i => i[0], i => i[1])
					.ToDictionary(i => i.Key, i => i.First())
			};
		}
		public static MachineDefinition CreateSimulateMachineDefinition(string name = "",string path = @"Matrikon.OPC.Simulation.1@DOGMEM")
		{
			var id = Utilities.NewId();
			return new MachineDefinition()
			{
				Id = id,
				Name = string.IsNullOrEmpty(name) ? id : name,
				Path = path,
				Enabled = true,
				Type = MachineType.Siunmerik,
				Tags = Resource1.SimulateOPCTagString.Split('\n')
					.Select(i =>
					{
						var a = i.Split('=');
						return new[] { a[0].Trim(), a[1].Trim() };
					})
					.ToLookup(i=>i[0],i=>i[1])
					.ToDictionary(i => i.Key, i => i.First())
			};
		}

		
	}
}
